home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / dobbs / v17n02 / 3d.exe / L2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-22  |  1.6 KB  |  37 lines

  1. /* Transforms all vertices in the specified object into view space,
  2.    then perspective projects them to screen space and maps them to
  3.    screen coordinates, storing the results in the object. */
  4. #include <math.h>
  5. #include "polygon.h"
  6.  
  7. void XformAndProjectPoints(double Xform[4][4],
  8.    struct Object * ObjectToXform)
  9. {
  10.    int i, NumPoints = ObjectToXform->NumVerts;
  11.    struct Point3 * Points = ObjectToXform->VertexList;
  12.    struct Point3 * XformedPoints = ObjectToXform->XformedVertexList;
  13.    struct Point3 * ProjectedPoints =
  14.          ObjectToXform->ProjectedVertexList;
  15.    struct Point * ScreenPoints = ObjectToXform->ScreenVertexList;
  16.  
  17.    for (i=0; i<NumPoints; i++, Points++, XformedPoints++,
  18.          ProjectedPoints++, ScreenPoints++) {
  19.       /* Transform to view space */
  20.       XformVec(Xform, (double *)Points, (double *)XformedPoints);
  21.       /* Perspective-project to screen space */
  22.       ProjectedPoints->X = XformedPoints->X / XformedPoints->Z *
  23.             PROJECTION_RATIO * (SCREEN_WIDTH / 2.0);
  24.       ProjectedPoints->Y = XformedPoints->Y / XformedPoints->Z *
  25.             PROJECTION_RATIO * (SCREEN_WIDTH / 2.0);
  26.       ProjectedPoints->Z = XformedPoints->Z;
  27.       /* Convert to screen coordinates. The Y coord is negated to
  28.          flip from increasing Y being up to increasing Y being down,
  29.          as expected by the polygon filler. Add in half the screen
  30.          width and height to center on the screen */
  31.       ScreenPoints->X = ((int) floor(ProjectedPoints->X + 0.5)) +
  32.                SCREEN_WIDTH/2;
  33.       ScreenPoints->Y = (-((int) floor(ProjectedPoints->Y + 0.5))) +
  34.                SCREEN_HEIGHT/2;
  35.    }
  36. }
  37.